package listbyorder.access101_200.test169;

import java.util.HashMap;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/14 13:38
 */
public class Solution2 {

    // 使用HashMap实现O（N）的时间复杂度
    public int majorityElement(int[] nums) {
        HashMap<Integer, Integer> map = new HashMap<>();
        int n = nums.length;
        for (int i = 0; i < nums.length; i++) {
            int before = map.getOrDefault(nums[i], 0) + 1;
            if (before == n / 2) {
                return nums[i];
            }
            map.put(nums[i], before + 1);
        }
        return -1;
    }
}
